GtkGrid: Add a way to insert rows or columns
authorMatthias Clasen <mclasen@redhat.com>
Mon, 8 Aug 2011 10:06:59 +0000 (12:06 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 8 Aug 2011 11:13:48 +0000 (13:13 +0200)
This is useful functionality that makes it easier to insert
things in the middle of an already populated grid.

Bug 653817

docs/reference/gtk/gtk3-sections.txt
gtk/gtk.symbols
gtk/gtkgrid.c
gtk/gtkgrid.h
tests/testgrid.c

index 0167abb005b3c1633c7bd7c606841062230e5c15..7992f8faedbcaea0b5add52d22ca446d05de5e33 100644 (file)
@@ -6914,6 +6914,9 @@ GtkGrid
 gtk_grid_new
 gtk_grid_attach
 gtk_grid_attach_next_to
+gtk_grid_insert_row
+gtk_grid_insert_column
+gtk_grid_insert_next_to
 gtk_grid_set_row_homogeneous
 gtk_grid_get_row_homogeneous
 gtk_grid_set_row_spacing
index a2447f797643a7156779805f67c71461177448c0..4325f2f12f0a50f35d10aa2af760c6f65ee50b63 100644 (file)
@@ -1090,6 +1090,9 @@ gtk_grid_get_column_spacing
 gtk_grid_get_row_homogeneous
 gtk_grid_get_row_spacing
 gtk_grid_get_type
+gtk_grid_insert_column
+gtk_grid_insert_next_to
+gtk_grid_insert_row
 gtk_grid_new
 gtk_grid_set_column_homogeneous
 gtk_grid_set_column_spacing
index 88fe59de3ae8450fc6e8db75f2afdd33a4897db0..20ec6882948ae3921e7ffa850ca23d1fd09ccf54 100644 (file)
@@ -1479,6 +1479,142 @@ gtk_grid_attach_next_to (GtkGrid         *grid,
   grid_attach (grid, child, left, top, width, height);
 }
 
+/**
+ * gtk_grid_insert_row:
+ * @grid: a #GtkGrid
+ * @position: the position to insert the row at
+ *
+ * Inserts a row at the specified position.
+ *
+ * Children which are attached at or below this position
+ * are moved one row down. Children which span across this
+ * position are grown to span the new row.
+ *
+ * Since: 3.2
+ */
+void
+gtk_grid_insert_row (GtkGrid *grid,
+                     gint     position)
+{
+  GtkGridPrivate *priv = grid->priv;
+  GtkGridChild *child;
+  GList *list;
+  gint top, height;
+
+  g_return_if_fail (GTK_IS_GRID (grid));
+
+  for (list = priv->children; list; list = list->next)
+    {
+      child = list->data;
+
+      top = CHILD_TOP (child);
+      height = CHILD_HEIGHT (child);
+
+      if (top >= position)
+        {
+          CHILD_TOP (child) = top + 1;
+          gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "top-attach");
+        }
+      else if (top + height > position)
+        {
+          CHILD_HEIGHT (child) = height + 1;
+          gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "height");
+        }
+    }
+}
+
+/**
+ * gtk_grid_insert_column:
+ * @grid: a #GtkGrid
+ * @position: the position to insert the column at
+ *
+ * Inserts a column at the specified position.
+ *
+ * Children which are attached at or to the right of this position
+ * are moved one column to the right. Children which span across this
+ * position are grown to span the new column.
+ *
+ * Since: 3.2
+ */
+void
+gtk_grid_insert_column (GtkGrid *grid,
+                        gint     position)
+{
+  GtkGridPrivate *priv = grid->priv;
+  GtkGridChild *child;
+  GList *list;
+  gint left, width;
+
+  g_return_if_fail (GTK_IS_GRID (grid));
+
+  for (list = priv->children; list; list = list->next)
+    {
+      child = list->data;
+
+      left = CHILD_LEFT (child);
+      width = CHILD_WIDTH (child);
+
+      if (left >= position)
+        {
+          CHILD_LEFT (child) = left + 1;
+          gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "left-attach");
+        }
+      else if (left + width > position)
+        {
+          CHILD_WIDTH (child) = width + 1;
+          gtk_container_child_notify (GTK_CONTAINER (grid), child->widget, "width");
+        }
+    }
+}
+
+/**
+ * gtk_grid_insert_next_to:
+ * @grid: a #GtkGrid
+ * @sibling: the child of @grid that the new row or column will be
+ *     placed next to
+ * @side: the side of @sibling that @child is positioned next to
+ *
+ * Inserts a row or column at the specified position.
+ *
+ * The new row or column is placed next to @sibling, on the side
+ * determined by @side. If @side is %GTK_POS_TOP or %GTK_POS_BOTTOM,
+ * a row is inserted. If @side is %GTK_POS_LEFT of %GTK_POS_RIGHT,
+ * a column is inserted.
+ *
+ * Since: 3.2
+ */
+void
+gtk_grid_insert_next_to (GtkGrid         *grid,
+                         GtkWidget       *sibling,
+                         GtkPositionType  side)
+{
+  GtkGridChild *child;
+
+  g_return_if_fail (GTK_IS_GRID (grid));
+  g_return_if_fail (GTK_IS_WIDGET (sibling));
+  g_return_if_fail (gtk_widget_get_parent (sibling) == (GtkWidget*)grid);
+
+  child = find_grid_child (grid, sibling);
+
+  switch (side)
+    {
+    case GTK_POS_LEFT:
+      gtk_grid_insert_column (grid, CHILD_LEFT (child));
+      break;
+    case GTK_POS_RIGHT:
+      gtk_grid_insert_column (grid, CHILD_LEFT (child) + CHILD_WIDTH (child));
+      break;
+    case GTK_POS_TOP:
+      gtk_grid_insert_row (grid, CHILD_TOP (child));
+      break;
+    case GTK_POS_BOTTOM:
+      gtk_grid_insert_row (grid, CHILD_TOP (child) + CHILD_HEIGHT (child));
+      break;
+    default:
+      g_assert_not_reached ();
+    }
+}
+
 /**
  * gtk_grid_set_row_homogeneous:
  * @grid: a #GtkGrid
index 530ef0be25caa953c1b4e935aed441e93988455c..99f9e4e300d1f07992578114c2af546da335d70b 100644 (file)
@@ -79,6 +79,13 @@ void       gtk_grid_attach_next_to         (GtkGrid         *grid,
                                             GtkPositionType  side,
                                             gint             width,
                                             gint             height);
+void       gtk_grid_insert_row             (GtkGrid         *grid,
+                                            gint             position);
+void       gtk_grid_insert_column          (GtkGrid         *grid,
+                                            gint             position);
+void       gtk_grid_insert_next_to         (GtkGrid         *grid,
+                                            GtkWidget       *sibling,
+                                            GtkPositionType  side);
 void       gtk_grid_set_row_homogeneous    (GtkGrid         *grid,
                                             gboolean         homogeneous);
 gboolean   gtk_grid_get_row_homogeneous    (GtkGrid         *grid);
index f9e85bb99c6418de8932ac6a7a8178df46a50fc8..cb79b359f7326fade20be178feb524465468a95f 100644 (file)
@@ -247,6 +247,79 @@ scrolling (void)
   gtk_widget_show_all (window);
 }
 
+static void
+insert (void)
+{
+  GtkWidget *window;
+  GtkWidget *g;
+  GtkWidget *grid;
+  GtkWidget *child;
+
+  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  gtk_window_set_title (GTK_WINDOW (window), "Insertion");
+
+  g = gtk_grid_new ();
+  gtk_grid_set_row_spacing (GTK_GRID (g), 10);
+  gtk_grid_set_column_spacing (GTK_GRID (g), 10);
+  gtk_container_add (GTK_CONTAINER (window), g);
+
+  grid = gtk_grid_new ();
+  gtk_grid_attach (GTK_GRID (g), grid, 0, 0, 1, 1);
+
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 0)", "blue"), 0, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 1)", "blue"), 0, 1, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 0)", "green"), 1, 0, 1, 2);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(2, 0)", "yellow"), 2, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(2, 1)", "yellow"), 2, 1, 1, 1);
+
+  gtk_grid_insert_row (GTK_GRID (grid), 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 1)", "red"), 0, 1, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(2, 1)", "red"), 2, 1, 1, 1);
+
+  grid = gtk_grid_new ();
+  gtk_grid_attach (GTK_GRID (g), grid, 1, 0, 1, 1);
+
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 0)", "blue"), 0, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 0)", "blue"), 1, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 1)", "green"), 0, 1, 2, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 2)", "yellow"), 0, 2, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 2)", "yellow"), 1, 2, 1, 1);
+
+  gtk_grid_insert_column (GTK_GRID (grid), 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 0)", "red"), 1, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 2)", "red"), 1, 2, 1, 1);
+
+  grid = gtk_grid_new ();
+  gtk_grid_attach (GTK_GRID (g), grid, 0, 1, 1, 1);
+
+  child = test_widget ("(0, 0)", "blue");
+  gtk_grid_attach (GTK_GRID (grid), child, 0, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 1)", "blue"), 0, 1, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 0)", "green"), 1, 0, 1, 2);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(2, 0)", "yellow"), 2, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(2, 1)", "yellow"), 2, 1, 1, 1);
+
+  gtk_grid_insert_next_to (GTK_GRID (grid), child, GTK_POS_BOTTOM);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 1)", "red"), 0, 1, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(2, 1)", "red"), 2, 1, 1, 1);
+
+  grid = gtk_grid_new ();
+  gtk_grid_attach (GTK_GRID (g), grid, 1, 1, 1, 1);
+
+  child = test_widget ("(0, 0)", "blue");
+  gtk_grid_attach (GTK_GRID (grid), child, 0, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 0)", "blue"), 1, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 1)", "green"), 0, 1, 2, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(0, 2)", "yellow"), 0, 2, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 2)", "yellow"), 1, 2, 1, 1);
+
+  gtk_grid_insert_next_to (GTK_GRID (grid), child, GTK_POS_RIGHT);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 0)", "red"), 1, 0, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), test_widget ("(1, 2)", "red"), 1, 2, 1, 1);
+
+  gtk_widget_show_all (window);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -260,6 +333,7 @@ main (int argc, char *argv[])
   box_comparison ();
   empty_line ();
   scrolling ();
+  insert ();
 
   gtk_main ();